/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.wwm.DAO.impl;

import edu.fjut.se1603.wwm.DAO.BaseDao;
import edu.fjut.se1603.wwm.DAO.PetOwnerDao;
import edu.fjut.se1603.wwm.entity.PetOwner;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daydream
 */
public class PetOwnerDaoImpl extends BaseDao implements PetOwnerDao {

    @Override
    public int add(PetOwner owner) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("insert into petowner(ID,NAME,PASSWORD,MONEY) values(?,?,?,?)");
            pst.setInt(1, owner.getId());
            pst.setString(2, owner.getName());
            pst.setString(3, owner.getPassword());
            pst.setInt(4, owner.getMoney());
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public PetOwner getById(int id) {
        PetOwner owner = new PetOwner();
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from petowner where ID=?");
            pst.setInt(1, id);
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                owner.setId(id);
                owner.setName(rs.getString("NAME"));
                owner.setPassword(rs.getString("PASSWORD"));
                owner.setMoney(rs.getInt("MONEY"));
                con.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return owner;
    }

    @Override
    public List<PetOwner> getList() {
        List<PetOwner> owners = new ArrayList<PetOwner>();
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("select * from petowner");
            ResultSet rs = pst.executeQuery();
            while (rs.next()) {
                PetOwner owner = new PetOwner();
                owner.setId(rs.getInt("ID"));
                owner.setName(rs.getString("NAME"));
                owner.setPassword(rs.getString("PASSWORD"));
                owner.setMoney(rs.getInt("MONEY"));
                owners.add(owner);
            }
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return owners;
    }

    @Override
    public int delete(Integer id) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("delete from petowner where ID=" + id);
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public int change(PetOwner owner, Integer money,int type) {
        int r = 0;
        try {
            Connection con = BaseDao.getConnection();
            PreparedStatement pst = con.prepareStatement("update petowner set MONEY=? where ID=" + owner.getId());
            if(type==0){
                pst.setInt(1, (owner.getMoney()-money));
            }
            else if(type==1){
                pst.setInt(1, (owner.getMoney()+money));
            }
            r = pst.executeUpdate();
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(PetOwnerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

}
